package bs.app.android;

import android.graphics.Paint;
import android.graphics.Typeface;

public class Font {

	public static Font ins = null;
	
	public static final int FACE_SYSTEM = 0;
	public static final int FACE_MONOSPACE = 32;
	public static final int FACE_PROPORTIONAL = 64;
	
	public static final int STYLE_PLAIN = 0;
	public static final int STYLE_BOLD = 1;
	public static final int STYLE_ITALIC = 2;
	public static final int STYLE_UNDERLINED = 4;
	
	public static final int FONT_STATIC_TEXT = 0;
	public static final int FONT_INPUT_TEXT = 1;
	
	public static final int SIZE_SMALL = 8;
	public static final int SIZE_LARGE = 16;
	public static final int SIZE_MEDIUM = 0;

	private int curStyle = STYLE_PLAIN;
	private int curSize = SIZE_MEDIUM;
	private int curFace = FACE_SYSTEM;
	
	public static Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	
	Font(int face, int style, int size) {
		curFace = face;
		curSize = size;
		curStyle = style;
		ins = this;
		//namnv begin
		Typeface typefont = Typeface.createFromAsset(Static.App.getAssets(),"font/MyriadPro_Regular.otf");
		paint.setTypeface(typefont);
		//namnv end
	}
	
	/** Gets the Font used by the high level user interface for the fontSpecifier passed in. */
	public static Font getFont(int fontSpecifier){
		//TODO: fontSpecifier : Plz! Rewrite
		return getFont(FACE_SYSTEM, STYLE_PLAIN, SIZE_MEDIUM);
	}
	
	/** Obtains an object representing a font having the specified face, style, and size. */
	public static Font getFont(int face, int style, int size) {
		if(ins == null)
			ins = new Font(face, style, size);
		return ins;
	}
	
	public static Font 	getDefaultFont(){
		return getFont(0);
	}
	
	public int charWidth(char ch) {
		return (int) paint.measureText(new char[] { ch }, 0, 1);
	}

	public int charsWidth(char[] ch, int offset, int length) {
		return (int) paint.measureText(ch, offset, length);
	}

	public int getBaselinePosition() {
		return -paint.getFontMetricsInt().ascent;
	}

	public int getHeight() {
		return paint.getFontMetricsInt(paint.getFontMetricsInt());
	}

	public int stringWidth(String str) {
		return (int) paint.measureText(str);
	}
	
	public int getFace(){return curFace;}
	public int getSize(){return curSize;}
	public int getStyle(){return curStyle;}
	public boolean isBold(){ return (curStyle & STYLE_BOLD) == STYLE_BOLD;}
	public boolean isItalic(){return (curStyle & STYLE_ITALIC) == STYLE_ITALIC;}
	public boolean isPlain(){return (curStyle & STYLE_PLAIN) == STYLE_PLAIN;}
	public boolean isUnderlined(){return (curStyle & STYLE_UNDERLINED) == STYLE_UNDERLINED;}

	public int substringWidth(String str, int offset, int len){
		if(offset >= str.length())
			return 0;
		if(offset < 0)
			offset = 0;
		int ie = len;
		if(offset + len > str.length()){
			ie = str.length() - offset;
		}
		return stringWidth(str.substring(offset, offset + ie));
	}
}
